package com.cgw.room.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import com.cgw.room.DogControllerActivity;
import com.cgw.room.MainApp;

import java.util.Timer;

public class RockerButtonView extends View {
    int Radius = 150;
    int RadiusInner = 100;
    int rockerMode = -1;

    DogControllerActivity mActivity = null;

    private String[] ButtonNames = new String[4];

    RockerSafeListener mListener = null;

    public RockerButtonView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mActivity = (DogControllerActivity)context;
        ButtonNames[0] = "";
        ButtonNames[1] = "";
        ButtonNames[2] = "";
        ButtonNames[3] = "";

        mListener = new RockerSafeListener() {
            @Override
            public void onRockerClick(int position, boolean isDown) {
                Log.i("[Test]","onRockerClick : " + position + "  " + isDown);
            }

            @Override
            public void toggleLeftOrRight(boolean isShow) {
                Log.i("[Test]","toggleLeftOrRight");
            }
        };
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Radius = this.getWidth() / 2;
        RadiusInner = Radius / 2;

        float[] angleStart = new float[4];
        float angleOffset = 0.0f;
        angleStart[0] = 45 + angleOffset;
        angleStart[1] = 135 + angleOffset;
        angleStart[2] = 225 + angleOffset;
        angleStart[3] = 315 + angleOffset;
        float section = 90;

        int colorBack = Color.parseColor("#484848");
        Paint paintBack = new Paint();
        paintBack.setColor(colorBack);

        int colorButton = Color.parseColor("#555555");
        Paint paintButton = new Paint();
        paintButton.setColor(colorButton);

        int ButtoSelected = Color.parseColor("#1AB6FF");
        Paint paintSelected = new Paint();
        paintSelected.setColor(ButtoSelected);

        int TextSize = 50;
        int TextColor = Color.parseColor("#FFFFFF");
        Paint paintText = new Paint();
        paintText.setTextSize(TextSize);
        paintText.setColor(TextColor);

        int colorSection = Color.parseColor("#484848");
        Paint paintSection = new Paint();
        paintSection.setColor(colorSection);

        RectF rect = new RectF(0, 0, Radius * 2, Radius * 2);
        RectF rectSection = new RectF(Radius - RadiusInner, Radius - RadiusInner, Radius + RadiusInner, Radius + RadiusInner);

        canvas.drawArc(rect, 0, 360, true, paintBack);
        for (int n  = 0; n < 4; ++n){
            if (n == rockerMode){
                canvas.drawArc(rect, angleStart[n], section, true, paintSelected);
            }else{
                canvas.drawArc(rect, angleStart[n], section, true, paintButton);
            }
            canvas.drawArc(rectSection, angleStart[n], section, true, paintSection);
        }
        canvas.drawArc(rectSection, 0, 360, true, paintBack);

        for (int n  = 0; n < 4; ++n){
            canvas.save();
            {
                int len = (int)paintText.measureText(ButtonNames[n]);
                int startX = (Radius * 2 - len) / 2;
                if (n == 2) {
                    canvas.drawText(ButtonNames[n], startX, 100, paintText);
                }else{
                    if (n % 2 == 1){
                        canvas.rotate(360 - 90 * n, Radius, Radius);
                        canvas.drawText(ButtonNames[n], startX, 100, paintText);
                    }else{
                        canvas.rotate(360 - 90 * n, Radius, Radius);
                        canvas.drawText(ButtonNames[n], startX, Radius * 2 - 70, paintText);
                    }
                }
            }
            canvas.restore();
        }

        paintBack.setStrokeWidth(4);
        canvas.drawLine(5, 5, Radius * 2 - 5, Radius * 2 - 5, paintBack);
        canvas.drawLine(Radius * 2 - 5,5, 5,  Radius * 2 - 5, paintBack);

    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                matchingRegion((int) event.getX(), (int) event.getY());
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                mActivity.onRockerButtonClicked(RockerButtonView.this, rockerMode);
                rockerMode = -1;
                invalidate();
                break;
        }
        return true;
    }

    //判断点中的点在哪一个区域
    private void matchingRegion(int x, int y) {
        int xx = x - Radius;
        int yy = y - Radius;
        Log.i("[POS]", "Radius = " + Radius + "  RadiusInner = " + RadiusInner);

        Log.i("[POS]", "x = " + xx + "  y = " + yy + " fabs = " + (xx * xx + yy * yy) + " outter = " + (Radius * Radius * 4) + " inner = " + (RadiusInner * RadiusInner * 4));
        if (xx * xx + yy * yy > Radius * Radius) {
            Log.i("[POS]", "Outter");
            return;
        }

        if (xx * xx + yy * yy < RadiusInner * RadiusInner) {
            Log.i("[POS]", "Inner");
            return;
        }

        if (xx < yy && xx > -yy)
            rockerMode = 0;
        if (xx < yy && xx < -yy)
            rockerMode = 1;
        if (xx > yy && xx < -yy)
            rockerMode = 2;
        if (xx > yy && xx > -yy)
            rockerMode = 3;
    }

    public void setButtonNames(String[] buttonNames){
        if (buttonNames.length != 4){
            MainApp.toast("请设置正确的摇杆按钮名称。", 2000);
            return;
        }

        ButtonNames = buttonNames;
    }
}
